//
//  CustomCalloutView.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/10/15.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "CustomCalloutView.h"
#import <Masonry.h>

@interface CustomCalloutView ()

@property (nonatomic, strong) UIImageView *portraitView;
@property (nonatomic, strong) UITextView *subtitleLabel;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation CustomCalloutView

#define kArrorHeight        10
#define kPortraitMargin     5
#define kPortraitWidth      50
#define kPortraitHeight     50

#define kTitleWidth         120
#define kTitleHeight        20

- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self)
	{
		//self.backgroundColor = [UIColor clearColor];
		self.backgroundColor = [UIColor clearColor];
		[self initSubViews];
	}
	return self;
}

- (void)initSubViews
{
	// 添加图片，即商户图
	self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin, kPortraitWidth, kPortraitHeight)];
	
	self.portraitView.backgroundColor = [UIColor blackColor];
	[self addSubview:self.portraitView];
	
	// 添加标题，即商户个性签名
	self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin, kTitleWidth, kTitleHeight)];
	self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
	self.titleLabel.textColor = [UIColor whiteColor];
	self.titleLabel.text = @"titletitletitletitle";
	[self addSubview:self.titleLabel];
	
	// 添加副标题，即商户地址
	self.subtitleLabel = [[UITextView alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin * 2 + kTitleHeight, kTitleWidth, kTitleHeight)];
	self.subtitleLabel.font = [UIFont systemFontOfSize:12];
	self.subtitleLabel.textColor = [UIColor lightGrayColor];
	self.subtitleLabel.text = @"subtitleLabelsubtitleLabelsubtitleLabel";
	self.subtitleLabel.backgroundColor = kClearColor;
	self.subtitleLabel.editable = NO;
	[self addSubview:self.subtitleLabel];
}


- (void)drawRect:(CGRect)rect
{
	
	[self drawInContext:UIGraphicsGetCurrentContext()];
	
	self.layer.shadowColor = [[UIColor blackColor] CGColor];
	self.layer.shadowOpacity = 1.0;
	self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
	
}

- (void)drawInContext:(CGContextRef)context
{
	
	CGContextSetLineWidth(context, 2.0);
	CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.8].CGColor);
	
	[self getDrawPath:context];
	CGContextFillPath(context);
	
}

- (void)getDrawPath:(CGContextRef)context
{
	CGRect rrect = self.bounds;
	CGFloat radius = 6.0;
	CGFloat minx = CGRectGetMinX(rrect),
	midx = CGRectGetMidX(rrect),
	maxx = CGRectGetMaxX(rrect);
	CGFloat miny = CGRectGetMinY(rrect),
	maxy = CGRectGetMaxY(rrect)-kArrorHeight;
	
	CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
	CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
	CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
	
	CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
	CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
	CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
	CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
	CGContextClosePath(context);
}

- (void)setTitle:(NSString *)title
{
	self.titleLabel.text = title;
}

- (void)setSubtitle:(NSString *)subtitle
{
	self.subtitleLabel.text = subtitle;
	
	//一开始只有一行的时候是14.5。P.S由于自适应了，如果他没有签名的话会整个calloutview缩上去，所以只在有字的时候自适应。
	if (![NSString isEmpty:subtitle]) {
		[self.subtitleLabel sizeToFit];
		NSLog(@"subtitlelabel height is %lf", self.subtitleLabel.size.height);
		self.height = 55+self.subtitleLabel.size.height-14.5;
	}
	
	/*
	else if(subtitle.length >100){
		self.subtitleLabel.height = kTitleHeight+100;
		[self.subtitleLabel setNeedsFocusUpdate];
		self.height = 55+100;
	}
	 */
}

- (void)setImageStr:(NSString *)imageStr
{
	NSLog(@"img url is %@", imageStr);
	[self.portraitView sd_setImageWithURL:[NSURL URLWithString:imageStr]];
}


@end
